package com.kgc.kmall.kmallmanagerservice.service;

import com.alibaba.fastjson.JSON;
import com.kgc.kmall.bean.*;
import com.kgc.kmall.kmallmanagerservice.mapper.*;
import com.kgc.kmall.service.SpuService;
import com.kgc.kmall.util.RedisUtil;
import org.apache.dubbo.config.annotation.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class SpuServiceImpl implements SpuService {
    @Resource
    PmsProductInfoMapper pmsProductInfoMapper;
    @Resource
    PmsBaseSaleAttrMapper pmsBaseSaleAttrMapper;

    @Resource
    PmsProductImageMapper pmsProductImageMapper;

    @Resource
    PmsProductSaleAttrMapper pmsProductSaleAttrMapper;

    @Resource
    pmsProductSaleAttrValueMapper pmsProductSaleAttrValueMappers;

    @Resource
    RedisUtil redisUtil;

    @Override
    public List<PmsProductInfo> spuList(Long catalog3Id) {
        PmsProductInfoExample example=new PmsProductInfoExample();
        PmsProductInfoExample.Criteria criteria = example.createCriteria();
        criteria.andCatalog3IdEqualTo(catalog3Id);
        List<PmsProductInfo> infoList = pmsProductInfoMapper.selectByExample(example);
        return infoList;
    }

    //查询售卖属性
    @Override
    public List<PmsBaseSaleAttr> baseSaleAttrList() {

        return pmsBaseSaleAttrMapper.selectByExample(null);
    }

    //添加PmsProductInfo
    @Override
    public Integer addPmsProductInfo(PmsProductInfo pmsProductInfo) {
        int insert = pmsProductInfoMapper.insert(pmsProductInfo);
        Long productInfoId = pmsProductInfo.getId();
        //获取图片集合
        List<PmsProductImage> spuImageList = pmsProductInfo.getSpuImageList();
        if(spuImageList!=null&&spuImageList.size()>0){
            pmsProductImageMapper.insertProductImage(productInfoId,spuImageList);
        }
        //获取属性集合
        List<PmsProductSaleAttr> spuSaleAttrList = pmsProductInfo.getSpuSaleAttrList();
        if(spuSaleAttrList!=null&&spuSaleAttrList.size()>0){
            pmsProductSaleAttrMapper.insertProductattr(productInfoId,spuSaleAttrList);
            for (PmsProductSaleAttr pmsProductSaleAttr : spuSaleAttrList) {
                List<pmsProductSaleAttrValue> spuSaleAttrValueList = pmsProductSaleAttr.getSpuSaleAttrValueList();
                if(spuSaleAttrValueList!=null){
                    for (pmsProductSaleAttrValue pmsProductSaleAttrValue : spuSaleAttrValueList) {
                        pmsProductSaleAttrValue.setProductId(productInfoId);
                        pmsProductSaleAttrValueMappers.insert(pmsProductSaleAttrValue);
                    }
                }
            }
        }
        return insert;
    }


    @Override
    public List<PmsProductSaleAttr> spuSaleAttrList(Long spuId) {
        PmsProductSaleAttrExample example=new PmsProductSaleAttrExample();
        PmsProductSaleAttrExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(spuId);
        List<PmsProductSaleAttr> pmsProductSaleAttrs = pmsProductSaleAttrMapper.selectByExample(example);
        for (PmsProductSaleAttr pmsProductSaleAttr : pmsProductSaleAttrs) {
            pmsProductSaleAttrValueExample example1=new pmsProductSaleAttrValueExample();
            pmsProductSaleAttrValueExample.Criteria criterias=example1.createCriteria();
            criterias.andProductIdEqualTo(spuId);
            criterias.andSaleAttrIdEqualTo(pmsProductSaleAttr.getSaleAttrId());
            List<pmsProductSaleAttrValue> pmsProductSaleAttrValues = pmsProductSaleAttrValueMappers.selectByExample(example1);
            pmsProductSaleAttr.setSpuSaleAttrValueList(pmsProductSaleAttrValues);
        }
        return pmsProductSaleAttrs;
    }

    @Override
    public List<PmsProductImage> spuImageList(Long spuId) {
        PmsProductImageExample example=new PmsProductImageExample();
        PmsProductImageExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(spuId);
        List<PmsProductImage> pmsProductImages = pmsProductImageMapper.selectByExample(example);
        return pmsProductImages;
    }

    @Override
    public List<PmsProductSaleAttr> spuImageListBySpuIdAndSkuId(Long spuId, Long skuId) {
        String key="spu:"+spuId+":"+"skuId:"+skuId+":info";
        Jedis jedis=redisUtil.getJedis();
        String spuJSON = jedis.get(key);
        List<PmsProductSaleAttr> saleAttrList =null;
        if(spuJSON!=null){
            if(spuJSON.equals("empty")){
                return null;
            }
            saleAttrList = JSON.parseArray(spuJSON, PmsProductSaleAttr.class);
        }else{
            String skuLockey="spu:"+spuId+":"+"skuId:"+skuId+":lock";
            String skuLockValue= UUID.randomUUID().toString();
            String lock=jedis.set(skuLockey,skuLockValue,"NX","PX",60*1000);
            if(lock.equals("OK")&&lock!=null){
                saleAttrList = pmsProductSaleAttrMapper.spuSaleAttrListIsCheck(spuId, skuId);
                if(saleAttrList==null&&saleAttrList.size()<=0){
                    jedis.setex(key,1*60*1000,"empty");
                }else{
                    Random random=new Random();
                    int i = random.nextInt(10);
                    jedis.setex(key,i*60*1000,JSON.toJSONString(saleAttrList));
                }
            }else{
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            String skuLockValue2  = jedis.get(skuLockey);
            if(skuLockValue2!=null&&skuLockValue2 .equals(skuLockValue)){
            jedis.del(skuLockey);
            }
        }
        jedis.close();
        return saleAttrList;
    }
}
